﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{

    /// <summary>
    /// Summary description for Album
    /// </summary>
    public class Album
    {
        private int _AlbumID;
        private int _UserID;
        private String _AlbumName;
        private String _AlbumDescription;
        private DateTime _CreateAlbumDate;
        private DateTime _LastModifyDate;
        private bool _IsShown;
        private string _Path;

        public string Path
        {
            get
            {
                return _Path;
            }
            set
            {
                _Path = value;
            }
        }

        public int AlbumID
        {
            get
            {
                return _AlbumID;
            }
            set
            {
                _AlbumID = value;
            }
        }

        public int UserID
        {
            get
            {
                return _UserID;
            }
            set
            {
                _UserID = value;
            }
        }

        public String AlbumName
        {
            get
            {
                return _AlbumName;
            }
            set
            {
                _AlbumName = value;
            }
        }

        public String AlbumDescription
        {
            get
            {
                return _AlbumDescription;
            }
            set
            {
                _AlbumDescription = value;
            }
        }

        public DateTime CreateAlbumDate
        {
            get
            {
                return _CreateAlbumDate;
            }
            set
            {
                _CreateAlbumDate = value;
            }
        }

        public DateTime LastModifyDate
        {
            get
            {
                return _LastModifyDate;
            }
            set
            {
                _LastModifyDate = value;
            }
        }

        public bool IsShown
        {
            get
            {
                return _IsShown;
            }
            set
            {
                _IsShown = value;
            }
        }

        public Album()
        {
            //
            // TODO: Add constructor logic here
            //

            _AlbumID = -1;
            _UserID = -1;
            _AlbumName = null;
            _AlbumDescription = null;
            _CreateAlbumDate = DateTime.Now;
            _LastModifyDate = DateTime.Now;
            _IsShown = false;
            _Path = null;

        }

        public Album(SqlDataReader SqlReader)
        {
            if (SqlReader != null && !SqlReader.IsClosed)
            {
                _AlbumID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    _UserID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    _AlbumName = SqlReader.GetString(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    _AlbumDescription = SqlReader.GetString(3);
                }
                if (!SqlReader.IsDBNull(4))
                {
                    _Path = SqlReader.GetString(4);

                }
                if (!SqlReader.IsDBNull(5))
                {
                    _CreateAlbumDate = SqlReader.GetDateTime(5);

                }
                if (!SqlReader.IsDBNull(6))
                {
                    _LastModifyDate = SqlReader.GetDateTime(6);

                }
                if (!SqlReader.IsDBNull(7))
                {
                    _IsShown = SqlReader.GetBoolean(7);
                }
                if (!SqlReader.IsDBNull(8))
                {
                    _Path = SqlReader.GetString(8);
                }
            }
        }

        /// <summary>
        /// Get All
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAll()
        {
            DBHelp Dbh = new DBHelp();
            DataTable dt = new DataTable();
            String cmd = "Album_GetAll";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static DataTable GetByUserCreated(int UserID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@user_id", UserID);
            DataTable dt = new DataTable();
            String cmd = "select * from tbl_album where user_id = @user_id";
            dt = Dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        /// <summary>
        /// Get Info of Album
        /// </summary>
        /// <param name="AlbumID"></param>
        /// <returns></returns>
        public static Album GetInfo(int AlbumID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@album_id", AlbumID);
            Album album = new Album();
            string cmd = "Album_GetInfo";
            SqlDataReader SqlReader = Dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (SqlReader.Read())
            {
                album.AlbumID = SqlReader.GetInt32(0);
                if (!SqlReader.IsDBNull(1))
                {
                    album.UserID = SqlReader.GetInt32(1);
                }
                if (!SqlReader.IsDBNull(2))
                {
                    album.AlbumName = SqlReader.GetString(2);
                }
                if (!SqlReader.IsDBNull(3))
                {
                    album.AlbumDescription = SqlReader.GetString(3);
                }
                
                if (!SqlReader.IsDBNull(4))
                {
                    album.CreateAlbumDate = SqlReader.GetDateTime(4);

                }
                if (!SqlReader.IsDBNull(5))
                {
                    album.LastModifyDate = SqlReader.GetDateTime(5);

                }
                if (!SqlReader.IsDBNull(6))
                {
                    album.IsShown = SqlReader.GetBoolean(6);
                }
                if (!SqlReader.IsDBNull(7))
                {
                    album.Path = SqlReader.GetString(7);
                }
            }
            return album;
        }

        /// <summary>
        /// Add new Album to DB
        /// </summary>
        /// <param name="album"></param>
        public static int Insert(Album album)
        {
            DBHelp Dbh = new DBHelp();
            SqlParameter albumId = new SqlParameter();
            albumId.DbType = DbType.Int32;
            albumId.Direction = ParameterDirection.Output;
            albumId.ParameterName = @"@album_id";
            Dbh.AddParameter(albumId);
            Dbh.AddParameter("@user_id", album.UserID);
            Dbh.AddParameter("@album_name", album.AlbumName);
            Dbh.AddParameter("@description", album.AlbumDescription);
            Dbh.AddParameter("@created_date", album.CreateAlbumDate);
            Dbh.AddParameter("@modified_date", album.LastModifyDate);
            Dbh.AddParameter("@is_shown", album.IsShown);
            Dbh.AddParameter("@folder_path", album.Path);
            string cmd = "Album_Insert";
            try
            {
                Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
                return (int)albumId.Value;
            }
            catch (System.Exception Ex)
            {
                return -1;
            }
            
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="album"></param>
        public static void Update(Album album)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@album_id", album.AlbumID);
            Dbh.AddParameter("@user_id", album.UserID);
            Dbh.AddParameter("@album_name", album.AlbumName);
            Dbh.AddParameter("@description", album.AlbumDescription);
            Dbh.AddParameter("@created_date", album.CreateAlbumDate);
            Dbh.AddParameter("@modified_date", album.LastModifyDate);
            Dbh.AddParameter("@is_shown", album.IsShown);
            Dbh.AddParameter("@folder_path", album.Path);
            string cmd = "Album_Update";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        public static void Delete(int AlbumID)
        {
            DBHelp Dbh = new DBHelp();
            Dbh.AddParameter("@album_id", AlbumID);
            string cmd = "Album_Delete";
            Dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    }
}